package com.jerry.base.java8.hiorder;

import java.util.List;
import java.util.ListIterator;
import java.util.function.Consumer;

/**
 * 高阶函数-内循环
 *
 * @author Jerry
 * @since 2024/8/1 21:04
 */
public class InnerLoop {

    /**
     * 逆序遍历集合，只负责元素处理，不改变集合。
     *
     * @param args
     */
    public static void main(String[] args) {
        List<Integer> list = List.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
        hiOrder(list, System.out::println);
    }

    public static <T> void hiOrder(List<T> list, Consumer<T> consumer) {
        ListIterator<T> iterator = list.listIterator(list.size());
        while (iterator.hasPrevious()) {
            consumer.accept(iterator.previous());
        }
    }
}
